package com.cmos.controller;

import com.cmos.entity.Role;
import com.cmos.entity.RolePrivilege;
import com.cmos.service.RoleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("role")
public class RoleController {
    @Resource
    private RoleService roleService;

    //查询角色信息
    @RequestMapping("findById")
    public Map findById(String id) {
        Map map = new HashMap();
        try {
            Role role = roleService.queryRole(new Role().setId(id));
            map.put("status", 200);
            map.put("role", role);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //查询所有角色
    @RequestMapping("findAllRole")
    public Map findAllRole() {
        Map map = new HashMap();
        try {
            List<Role> roles = roleService.queryAllRole();
            map.put("status", 200);
            map.put("roles", roles);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //修改角色信息
    @RequestMapping("updateRole")
    public Map updateRole(Role role, String[] ids) {
        Map map = new HashMap();
        try {
            roleService.changeRole(role, ids);
            map.put("status", 200);
            map.put("message", "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //添加角色
    @RequestMapping("addRole")
    public Map addRole(Role role, String[] ids) {
        Map map = new HashMap();
        try {
            System.out.println(ids);
            System.out.println(role);
            role.setId(UUID.randomUUID().toString().replace("-", ""));
            roleService.saveRole(role, ids);
            map.put("status", 200);
            map.put("message", "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //删除角色
    @RequestMapping("delRole")
    public Map delRole(String id) {
        Map map = new HashMap();
        try {
            roleService.removeRole(id);
            //删除角色权限表
            roleService.removeRolePrivilege(id);
            map.put("status", 200);
            map.put("message", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //给角色增加权限
    public Integer addPrivilegeToRole(String roleId, String[] ids) {
        List<RolePrivilege> rolePrivileges = new ArrayList<>();
        for (String id : ids) {
            RolePrivilege rolePrivilege = new RolePrivilege();
            rolePrivilege.setId(UUID.randomUUID().toString().replace("-", ""))
                    .setRoleId(roleId).setPrivilegeId(id);
            rolePrivileges.add(rolePrivilege);
        }
        return roleService.saveRolePrivilege(rolePrivileges);
    }

    //修改角色权限
    public Integer delPrivilegeForRole(String roleId, String[] ids) {
        //删除角色权限
        roleService.removeRolePrivilege(roleId);
        //为角色重新添加权限
        return addPrivilegeToRole(roleId, ids);
    }
}
